<?php
namespace app\api\controller;

use app\common\controller\BaseController;
use app\common\repositories\apps\AppsAccessRepository;
use app\common\repositories\apps\AppsAppsRepository;
use app\common\repositories\apps\AppsEventRepository;
use app\common\repositories\apps\AppsRepository;
use app\common\repositories\apps\AppsWebRepository;
use support\Container;

/**
 * Apps
 */
class AppsController extends BaseController
{
    protected $repository;

    public function __construct()
    {
        $this->repository = new AppsRepository();
    }

    /**
     * @desc: get list
     * @author: generate
     */
    public function list()
    {
        $user_id = request()->user->id ?? 0;
        $where = request()->get();
        $where['status'] = 1;
        $where['is_del'] = 0;
        $where['user_id'] = $user_id;


        [$page,$Limit]  = $this->getPage();
        $data = $this->repository->getList($where, $page, $Limit);
        $result = ['code' => 200, 'data' => $data];
        return json($result);
    }

    /**
     * @desc: create one Apps
     * @param varchar $name                       
     * @param varchar $android_down_url   
     * @param varchar $android_scheme_url 
     * @param varchar $ios_down_url       
     * @param varchar $ios_scheme_url     
     * @param varchar $universal_links    
     * @param varchar $package_name       
     * @param tinyint $is_huawei          
     * @param tinyint $is_xiaomi          
     * @param tinyint $is_vivo            
     * @param tinyint $is_oppo            
     * @param tinyint $is_honour          
     * @param tinyint $is_yyb             
     * @param varchar $icon               
     * @param varchar $banner             
     * @param int     $user_id            
     * @param array     $stores   
     * @author: generate
     */
    public function create()
    {
        $user_id = request()->user->id ?? 0;
        $name = request()->post('name','');
        $android_down_url = request()->post('android_down_url','');
        $android_scheme_url = request()->post('android_scheme_url','');
        $ios_down_url = request()->post('ios_down_url','');
        $ios_scheme_url = request()->post('ios_scheme_url','');
        $universal_links = request()->post('universal_links','');
        $package_name = request()->post('package_name','');
        $is_huawei = request()->post('is_huawei',0);
        $is_xiaomi = request()->post('is_xiaomi',0);
        $is_vivo = request()->post('is_vivo',0);
        $is_oppo = request()->post('is_oppo',0);
        $is_honour = request()->post('is_honour',0);
        $is_yyb = request()->post('is_yyb',0);
        $icon = request()->post('icon','');
        $banner = request()->post('banner','');
        $stores = request()->post('stores',[]);

        $huawei_down_url = request()->post('huawei_down_url','');
        $xiaomi_down_url = request()->post('xiaomi_down_url','');
        $vivo_down_url = request()->post('vivo_down_url','');
        $oppo_down_url = request()->post('oppo_down_url','');
        $honour_down_url = request()->post('honour_down_url','');
        $yyb_down_url = request()->post('yyb_down_url','');

        $result = $this->repository->createOneApps(
            $name,
            $android_down_url,
            $android_scheme_url,
            $ios_down_url,
            $ios_scheme_url,
            $universal_links,
            $package_name,
            $is_huawei,
            $is_xiaomi,
            $is_vivo,
            $is_oppo,
            $is_honour,
            $is_yyb,
            $icon,
            $banner,
            $user_id,
            $stores,
            $huawei_down_url,
            $xiaomi_down_url,
            $vivo_down_url,
            $oppo_down_url,
            $honour_down_url,
            $yyb_down_url
        );
        return json($result);
    }

    /**
     * @desc: update one Apps
     * @param varchar $name               
     * @param varchar $appkey             
     * @param varchar $android_down_url   
     * @param varchar $android_scheme_url 
     * @param varchar $ios_down_url       
     * @param varchar $ios_scheme_url     
     * @param varchar $universal_links    
     * @param varchar $package_name       
     * @param tinyint $is_huawei          
     * @param tinyint $is_xiaomi          
     * @param tinyint $is_vivo            
     * @param tinyint $is_oppo            
     * @param tinyint $is_honour          
     * @param tinyint $is_yyb             
     * @param varchar $icon               
     * @param varchar $banner             
     * @param int     $user_id      
     * @param array     $stores       
     * @author: generate
     */
    public function update($id)
    {
        $user_id = request()->user->id ?? 0;
        $name = request()->post('name','');
        $android_down_url = request()->post('android_down_url','');
        $android_scheme_url = request()->post('android_scheme_url','');
        $ios_down_url = request()->post('ios_down_url','');
        $ios_scheme_url = request()->post('ios_scheme_url','');
        $universal_links = request()->post('universal_links','');
        $package_name = request()->post('package_name','');
        $is_huawei = request()->post('is_huawei',0);
        $is_xiaomi = request()->post('is_xiaomi',0);
        $is_vivo = request()->post('is_vivo',0);
        $is_oppo = request()->post('is_oppo',0);
        $is_honour = request()->post('is_honour',0);
        $is_yyb = request()->post('is_yyb',0);
        $icon = request()->post('icon','');
        $banner = request()->post('banner','');
        $stores = request()->post('stores',[]);

        $huawei_down_url = request()->post('huawei_down_url','');
        $xiaomi_down_url = request()->post('xiaomi_down_url','');
        $vivo_down_url = request()->post('vivo_down_url','');
        $oppo_down_url = request()->post('oppo_down_url','');
        $honour_down_url = request()->post('honour_down_url','');
        $yyb_down_url = request()->post('yyb_down_url','');

        $result = $this->repository->updateOneApps(
            $id, 
            $name,
            $android_down_url,
            $android_scheme_url,
            $ios_down_url,
            $ios_scheme_url,
            $universal_links,
            $package_name,
            $is_huawei,
            $is_xiaomi,
            $is_vivo,
            $is_oppo,
            $is_honour,
            $is_yyb,
            $icon,
            $banner,
            $user_id,
            $stores,
            $huawei_down_url,
            $xiaomi_down_url,
            $vivo_down_url,
            $oppo_down_url,
            $honour_down_url,
            $yyb_down_url
        );
        return json($result);
    }
    /**
     * @desc: get one Apps detail
     * @author: generate
     */
    public function detail($id){
        $data = $this->repository->getDetail($id);
        $result = ['code' => 200, 'data' => $data];
        return json($result);
    }
    /**
     * @desc: statistics
     * @author: xiaohp
     */
    public function statistics($id){
        $data = $this->repository->statistics($id);
        $result = ['code' => 200, 'data' => $data];
        return json($result);
    }

    /**
     * @desc: get info
     * @author: generate
     */
    public function info(){
        $appinfo = request()->appinfo ?? [];
        $appkey = request()->appkey ?? '';
        $version = request()->version ?? '';
        $fingerprint = request()->fingerprint ?? '';
        $customInfo = request()->post('customInfo',[]);
        $deviceInfo = request()->post('deviceInfo',[]);
        if($appkey && $fingerprint && $version){
            Container::make(AppsWebRepository::class)->createOneAppsWeb($appkey,$deviceInfo,$customInfo,$fingerprint,$version);
        }
        $result = ['code' => 200, 'data' => $appinfo];
        return json($result);
    }

    /**
     * @desc: get custom
     * @author: generate
     */
    public function custom(){
        $appinfo = request()->appinfo ?? [];
        $appkey = request()->appkey ?? '';
        $version = request()->version ?? '';
        $fingerprint = request()->fingerprint ?? '';
        $deviceInfo = request()->post('deviceInfo',[]);
        if($appkey && $fingerprint && $version){
            Container::make(AppsAppsRepository::class)->createOneAppsApps($appkey,$deviceInfo,$fingerprint,$version);
        }
        $custom = [];
        if($appkey && $fingerprint && $version){
            $custom =  Container::make(AppsWebRepository::class)->getFirstCustom($appkey,$fingerprint);
        }
        $result = ['code' => 200, 'data' => ['custom' => $custom,'appinfo' => $appinfo,'version' => $version,'fingerprint' => $fingerprint,'appkey' => $appkey]];
        return json($result);
    }
    /**
     * @desc: get event
     * @author: generate
     */
    public function event(){
        $appinfo = request()->appinfo ?? [];
        $appkey = request()->appkey ?? '';
        $version = request()->version ?? '';
        $fingerprint = request()->fingerprint ?? '';
        $deviceInfo = request()->post('deviceInfo',[]);
        if($appkey && $fingerprint && $version){
            Container::make(AppsEventRepository::class)->createOneAppsEvent($appkey,$deviceInfo,$fingerprint,$version);
        }
        $result = ['code' => 200, 'data' => ['appinfo' => $appinfo,'version' => $version,'fingerprint' => $fingerprint,'appkey' => $appkey]];
        return json($result);
    }

}